package com.scoresystem.utils;

import java.util.ArrayList;
import java.util.List;

import com.scoresystem.model.Score;
import com.scoresystem.model.StuRemark;
import com.scoresystem.model.StuScore;
import com.scoresystem.model.Student;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class JsonUtil {
	public static String getState(JSONObject json){
		JSONObject object=JSONObject.fromObject(json);
		
		JSONObject obj= object.getJSONObject("meta");
		return obj.get("state").toString();
	}
	
	public static List<Student> getStudents(JSONObject json){
		List<Student> list=new ArrayList<Student>();
		JSONObject object=JSONObject.fromObject(json);
		
		JSONArray arr=object.getJSONArray("data");
		for(int i=0;i<arr.size();i++){
			JSONObject mark = (JSONObject)arr.get(i);
			Student student=new Student();
			student.setStunum((String)mark.get("stunum"));
			student.setStuname((String)mark.get("stuname"));
			student.setStuclass((String)mark.get("stuclass"));
			student.setMajor((String)mark.get("major"));
			list.add(student);
		}
		return list;
	}
	/**
	 * 解析总成绩表
	 * @param json
	 * @return
	 */
	public static List<StuScore> getStuScore(JSONObject json){
		List<StuScore> list=new ArrayList<StuScore>();
		JSONObject object=JSONObject.fromObject(json);
		
		JSONArray arr=object.getJSONArray("data");
		for(int i=0;i<arr.size();i++){
			JSONObject mark = (JSONObject)arr.get(i);
			StuScore score=new StuScore();
			score.setStunum((String)mark.get("stunum"));
			score.setStuname((String)mark.get("stuname"));
			score.setScore((Integer)mark.get("score"));
			if(score.getScore()>=50)
				score.setGpa((score.getScore()-50)/10.0);
			else
				score.setGpa(0.0);
			list.add(score);
		}
		return list;
	}
	/**
	 * 解析单个成绩表
	 */
	public static List<Score> getScore(JSONObject json){
		List<Score> list=new ArrayList<Score>();
		JSONObject object=JSONObject.fromObject(json);
		JSONArray arr=object.getJSONArray("data");
		for(int i=0;i<arr.size();i++){
			JSONObject mark = (JSONObject)arr.get(i);
			Score score=new Score();
			score.setCoursename((String)mark.get("coursename"));
			score.setScore((Integer)mark.get("score"));
			score.setWorknum((String)mark.get("worknum"));
			list.add(score);
		}
		return list;
	}
	
	public static List<StuRemark> getRemarks(JSONObject json){
		List<StuRemark> list=new ArrayList<StuRemark>();
		JSONObject object=JSONObject.fromObject(json);
		JSONArray arr=object.getJSONArray("data");
		for(int i=0;i<arr.size();i++){
			JSONObject mark=(JSONObject)arr.get(i);
			StuRemark sr=new StuRemark();
			sr.setStunum((String)mark.get("stunum"));
			sr.setRemark((String)mark.get("remark"));
			list.add(sr);
		}
		return list;
	}
}
